<?php
!defined('P_W') && exit('Forbidden');

/**
 * 图片DAO类
 * @author bingbing.renbb@2010-8-25
 *
 */
class PW_House_SecondimageDB extends BaseDB {
	/**
	 * @var string	$_tableName 数据表名称
	 * @var integer $_perPage	每页显示行数
	 * @var string  $_primaryKey 主键名称
	 */
	var $_tableName = 'pw_house_secondimage';
	var $_perPage = 14;
	var $_primaryKey = 'imageid';
	
	/**
	 * 新增多张图片
	 * @param	array	$data	各字段的值，字段名为键名
	 * @return	integer			插入的行数
	 */
	function addImages($data) {
		$data = $this->_checkData($data);
		if (!S::isArray($data)) return NULL;
		$_sql = 'INSERT INTO ' . $this->_tableName . ' (type, houseid, size, path, uploadtime, descrip, creator, name) VALUES ' . pwSqlMulti($data);
		$this->_db->update($_sql);
		return $this->_db->affected_rows();	
	}
	
	/**
	 * 新增一张图片
	 * @param	array	$data	各字段的值，字段名为键名
	 * @return	integer			插入的ID
	 */
	function addImage($data) {
		$data = $this->_cookData($data);
		if (!S::isArray($data)) return NULL;
		$_sql = 'INSERT INTO ' . $this->_tableName . ' (type, houseid, size, path, uploadtime, descrip, creator, name) VALUES (' . $this->_getImplodeString($data) . ')';
		$this->_db->update($_sql);
		return $this->_db->insert_id();
	}
	 
	/**
	 * 更新图片的类型或描述
	 * @param	integer	$id 	要更新的图片ID
	 * @param	array   $data	图片信息
	 * @return  integer 		影响的行数
	 */
	function updateImage($id, $data) {
		if (!is_numeric($id)) return NULL;
		$data = $this->_cookData($data);
		$_sql = 'UPDATE ' . $this->_tableName . ' SET  ' . $this->_getUpdateSqlString($data) . ' WHERE ' . $this->_primaryKey . '=' . $this->_addSlashes($id) . " LIMIT 1";
		$this->_db->update($_sql);
		return $this->_db->affected_rows();
	}
	
	/**
	 * 删除一张图片
	 * @param	mixed	$id 	要删除的记录ID
	 * @return  integer			影响的行数
	 */
	function deleteImage($id) {
		if (!is_numeric($id)) return NULL;
		$_sql = 'DELETE FROM ' . $this->_tableName . ' WHERE imageid =' . $this->_addSlashes($id);
		$this->_db->update($_sql);
		return $this->_db->affected_rows();
	}
	
	/**
	 * 删除多张图片
	 * @param	mixed	$ids 	要删除的记录ID
	 * @return  integer			影响的行数
	 */
	function deleteImages($ids) {
		if (!S::isArray($ids)) return NULL;
		$_sql = 'DELETE FROM ' . $this->_tableName . ' WHERE imageid IN (' . $this->_getImplodeString($ids) . ')';
		$this->_db->update($_sql);
		return $this->_db->affected_rows();
	}
	
	/**
	 * 根据图片类型取得图片
	 * @param	integer	$type	图片类型
	 * @param	integer	$page	分页中的第几页
	 * @return	array	$result	查询结果 
	 */
	function getImages($type, $page) {
		if (!is_numeric($type) || !is_numeric($page)) return array();
		$result = array();
		$page = (intval($page) > 1) ? intval($page) - 1 : 0;
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE type = ' . $this->_addSlashes($type) . ' ORDER BY uploadtime DESC' . $this->_Limit($page*$this->_perPage, $this->_perPage);
		$queryResult = $this->_db->query($_sql);
		while ($tempResult = $this->_db->fetch_array($queryResult)) {
			$result[] = $tempResult;
		}
		return $result;
	}
	
	/**
	 * 取得所有图片
	 * @param	integer	$page	分页中的第几页
	 * @return	array	$result	查询结果 
	 */
	function getAllImages($page) {
		if (!is_numeric($page)) return array();
		$result = array();
		$page = (intval($page) > 1) ? intval($page) - 1 : 0;
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' ORDER BY uploadtime DESC' . $this->_Limit($page*$this->_perPage, $this->_perPage);
		$queryResult = $this->_db->query($_sql);
		while ($tempResult = $this->_db->fetch_array($queryResult)) {
			$result[] = $tempResult;
		}
		return $result;
	}
	
	/**
	 * 根据图片id取得图片
	 * @param	integer	$id	图片的ID
	 * @return	array		查询结果 
	 */
	function getImageById($id) {
		if (!is_numeric($id)) return NULL;
		return $this->_get($id);	
	}
	
	/**
	 * 取得一个房子下的前几张图片
	 * @param integer $houseid
	 * @param integer $number
	 */
	function getTopImages($houseid, $type, $number) {
		if (!is_numeric($houseid) || $houseid < 1 || !is_numeric($number) || !is_numeric($type)) return array();
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid = ' . $this->_addSlashes($houseid) . ' AND type = ' . $this->_addSlashes($type) . ' ORDER BY uploadtime DESC' . $this->_Limit(0, $number);
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}
	
	/**
	 * 根据房子id跟类型来取得图片
	 * @param integer $houseid
	 * @param integer $type
	 */
	function getImagesByHidAndType($houseid, $type) {
		if (!is_numeric($houseid) || $houseid < 1 || !is_numeric($type)) return array();
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid = ' . $this->_addSlashes($houseid) . ' AND type = ' . $this->_addSlashes($type);
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}
	
	/**
	 * 根据多个房子id和类型来取得图片
	 * @param array $houseids
	 * @param integer $type
	 */
	function getImagesByHidsAndType($houseids, $type) {
		if (!S::isArray($houseids) || !is_numeric($type)) return array();
		$_sql = 'SELECT * FROM ' . $this->_tableName . ' WHERE houseid IN (' . $this->_getImplodeString($houseids) . ') AND type = ' . $this->_addSlashes($type);
		$query = $this->_db->query($_sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}
	
	/**
	 * 统计所有图片数目
	 * @return	integer	$number	记录数 
	 */
	function count() {
		$number = 0;
		$_sql = 'SELECT COUNT(*) AS sum FROM ' . $this->_tableName;
		$number = $this->_db->get_value($_sql);
		return $number;
	}
	
	/**
	 * 根据类型统计图片数目
	 * @param	integer	$type	图片类型
	 * @return	integer		$number	记录数 
	 */
	function countByType($type) {
		if (!is_numeric($type)) return NULL;
		$number = 0;
		$_sql = 'SELECT COUNT(*) AS sum FROM ' . $this->_tableName . ' WHERE type = ' . $this->_addSlashes($type);
		$number = $this->_db->get_value($_sql);
		return $number;	
	}
	
	/**
	 * 统计一个房子下的所有图片
	 * @param	integer	$houseid	房子id
	 * @param   integer $type		类型
	 * @return	integer		$number	记录数 
	 */
	function countByHouseid($houseid, $type) {
		if (!is_numeric($houseid) || !is_numeric($type)) return NULL;
		$number = 0;
		$_sql = 'SELECT COUNT(*) AS sum FROM ' . $this->_tableName . ' WHERE houseid = ' . $this->_addSlashes($houseid) . ' AND type = ' . $this->_addSlashes($type);
		$number = $this->_db->get_value($_sql);
		return $number;
	}
	
	/**
	 * 过滤二维数组
	 * @param	array	$data	各字段值
	 * @return	array	过滤后的数据 
	 */
	function _checkData($data) {
		if (!S::isArray($data)) return array(); 
		foreach ($data as $key => &$value) {
			is_array($value) && $value = $this->_cookData($value);
		}
		return $data;
	}
	
	/**
	 * 过滤一维数组
	 * @param	array	$data	各字段值
	 * @return	array	过滤后的数据 
	 */
	function _cookData($data) {
		if (!S::isArray($data)) return array(); 
		$allowFields = array('type', 'houseid', 'size', 'path', 'uploadtime', 'descrip', 'creator', 'name');
		$data = $this->_checkAllowField($data, $allowFields);
		$data['type'] = $data['type'] == 'sale' ? 0 : 1;
		(strpos($data['path'], 'attachment/') !== false) && $data['path'] = str_ireplace('attachment/', '', $data['path']);
		$data['path'] = Pcv($data['path']);
		$data['descrip'] = S::htmlEscape($data['descrip']);
		return $data;
	}
}
?>